package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.AllRankAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.PrizeRanksBean;
import com.yiwei.jmx.api.model.PrizeIndexModel;
import com.yiwei.jmx.api.model.PrizeRanksModel;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.view.ConfirmPopWindow;
import com.yiwei.jmx.view.V19FrameLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class AllRankActivity extends BaseActivity implements NestedScrollView.OnScrollChangeListener {

    @BindView(R.id.txt_title)
    TextView txtTitle;

    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_menu_one)
    SuperButton btnMenuOne;
    @BindView(R.id.btn_menu_two)
    SuperButton btnMenuTwo;
    @BindView(R.id.img_level_two)
    ImageView imgLevelTwo;
    @BindView(R.id.txt_level_two)
    TextView txtLevelTwo;
    @BindView(R.id.img_level_one)
    ImageView imgLevelOne;
    @BindView(R.id.txt_level_one)
    TextView txtLevelOne;
    @BindView(R.id.img_level_three)
    ImageView imgLevelThree;
    @BindView(R.id.txt_level_three)
    TextView txtLevelThree;
    @BindView(R.id.layout_icon_two)
    LinearLayout layoutIconTwo;
    @BindView(R.id.layout_money_title)
    LinearLayout layoutMoneyTitle;
    @BindView(R.id.list_three)
    RecyclerView listThree;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.layout_has_data)
    LinearLayout layoutHasData;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private int currentPage = 1;
    private boolean isOkRefresh = false;
    private Unbinder unbiner;
    private AllRankAdapter adapter;
    private ConfirmPopWindow confirmPopWindow;
    private String keyword = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_rank);
        initBar();
        setBarClick();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("全部排行");
        scrollView.setOnScrollChangeListener(this);
        adapter = new AllRankAdapter(this);
        listThree.setAdapter(adapter);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getPrizeRank();
            }
        });
        confirmPopWindow = new ConfirmPopWindow(this);
        confirmPopWindow.setOnChooseListener(new ConfirmPopWindow.OnChooseListener() {
            @Override
            public void onSuccess(String number) {
                keyword = number;
                getPrizeRank();
            }
        });
        getPrizeRank();
        ArrayList<PrizeIndexModel.DataBean.WeekListBean> data = getIntent().getParcelableArrayListExtra("data");
        if (data != null) {
            confirmPopWindow.setData(data);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    public void getPrizeRank() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        PrizeRanksBean bean = new PrizeRanksBean();
        bean.week_num = keyword;
        Api.api_service.getPrizeRank(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setUpdatePirze(result.data);
                });
    }

    private void setUpdatePirze(List<PrizeRanksModel.DataBean> reward_list) {
        //        奖金排行榜
//        List<PrizeRanksModel.DataBean> reward_list =data.reward_list;
        if (reward_list != null && reward_list.size() != 0) {
            layoutHasData.setVisibility(View.VISIBLE);
            layoutEmpty.setVisibility(View.GONE);
            //                      奖金大神磅  排版前三名
            if (reward_list.size() == 1) {
                imgLevelOne.setVisibility(View.VISIBLE);
//                            Glide.with(getContext()).load(reward_list.get(0).header).error(R.mipmap.img_invite_level_one).into(imgLevelOne);
                txtLevelOne.setText(reward_list.get(0).real_name + "\n￥" + reward_list.get(0).total_reward);
                imgLevelTwo.setVisibility(View.INVISIBLE);
                txtLevelTwo.setText("");
                imgLevelThree.setVisibility(View.INVISIBLE);
                txtLevelThree.setText("");
                adapter.update(new ArrayList<>());
            } else if (reward_list.size() == 2) {
                imgLevelOne.setVisibility(View.VISIBLE);
//                            Glide.with(getContext()).load(reward_list.get(0).header).error(R.mipmap.img_invite_level_one).into(imgLevelOne);
                txtLevelOne.setText(reward_list.get(0).real_name + "\n￥" + reward_list.get(0).total_reward);
                imgLevelTwo.setVisibility(View.VISIBLE);
//                            Glide.with(getContext()).load(reward_list.get(1).header).error(R.mipmap.img_invite_level_two).into(imgLevelTwo);
                txtLevelTwo.setText(reward_list.get(1).real_name + "\n￥" + reward_list.get(1).total_reward);
                imgLevelThree.setVisibility(View.INVISIBLE);
                txtLevelThree.setText("");
                adapter.update(new ArrayList<>());
            } else {
                imgLevelOne.setVisibility(View.VISIBLE);
//                            Glide.with(getContext()).load(reward_list.get(0).header).error(R.mipmap.img_invite_level_one).into(imgLevelOne);
                txtLevelOne.setText(reward_list.get(0).real_name + "\n￥" + reward_list.get(0).total_reward);
                imgLevelTwo.setVisibility(View.VISIBLE);
//                            Glide.with(getContext()).load(reward_list.get(1).header).error(R.mipmap.img_invite_level_two).into(imgLevelTwo);
                txtLevelTwo.setText(reward_list.get(1).real_name + "\n￥" + reward_list.get(1).total_reward);
                imgLevelThree.setVisibility(View.VISIBLE);
//                            Glide.with(getContext()).load(reward_list.get(2).header).error(R.mipmap.img_invite_level_three).into(imgLevelThree);
                txtLevelThree.setText(reward_list.get(2).real_name + "\n￥" + reward_list.get(2).total_reward);
                if (reward_list.size() > 3) {
                    reward_list.remove(0);
                    reward_list.remove(0);
                    reward_list.remove(0);
                    adapter.update(reward_list);
                } else {
                    adapter.update(new ArrayList<>());
                }
            }
        } else {
            layoutHasData.setVisibility(View.GONE);
            layoutEmpty.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({ R.id.btn_menu_one, R.id.btn_menu_two})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.btn_menu_one:
                btnMenuOne.setShapeSolidColor(0xff0072FF).setUseShape();
                btnMenuTwo.setShapeSolidColor(0xffffffff).setUseShape();
                btnMenuOne.setTextColor(0xffffffff);
                btnMenuTwo.setTextColor(0xff000000);
                confirmPopWindow.showAtBottom(btnMenuOne);
                break;
            case R.id.btn_menu_two:
                btnMenuOne.setShapeSolidColor(0xffffffff).setUseShape();
                btnMenuTwo.setShapeSolidColor(0xff0072FF).setUseShape();
                btnMenuOne.setTextColor(0xff000000);
                btnMenuTwo.setTextColor(0xffffffff);
                keyword = "";
                getPrizeRank();
                break;
        }
    }

    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int y, int oldScrollX, int oldScrollY) {
        if (y == (scrollView.getChildAt(0).getMeasuredHeight() - scrollView.getMeasuredHeight())) {
            //底部加载
            if (isOkRefresh) {
                isOkRefresh = false;
                currentPage++;
//                getPrizeRank(currentPage);
            }
        }
    }
}
